package hcQuest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Date;

import javax.swing.ComboBoxEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;

class FingerSequencePanel extends javax.swing.JPanel {
    /**
     * Hand Panel (c) O.Lindemann
     */

    private static final long serialVersionUID = 1L;
    private String dataDelim;

    private JPanel frg = new JPanel();
    private JPanel bkg = new JPanel();
    private JTextArea txtInstruct = new JTextArea();
    // private JTextField input1 = new JTextField();
    private MyComboBox cmBox1;
    private MyComboBox cmBox2;
    private MyComboBox cmBox3;
    private MyComboBox cmBox4;
    private MyComboBox cmBox5;
    private MyComboBox cmBox6;
    private MyComboBox cmBox7;
    private MyComboBox cmBox8;
    private MyComboBox cmBox9;
    private MyComboBox cmBox10;
    public JButton okButton = new JButton();
    private long presentTime = 0;

    public FingerSequencePanel(URL pictURL, String btnText,
	    Color BackgroundColor, String dataDelimiter) {
	dataDelim = dataDelimiter;
	cmBox1 = new MyComboBox(BackgroundColor);
	cmBox2 = new MyComboBox(BackgroundColor);
	cmBox3 = new MyComboBox(BackgroundColor);
	cmBox4 = new MyComboBox(BackgroundColor);
	cmBox5 = new MyComboBox(BackgroundColor);
	cmBox6 = new MyComboBox(BackgroundColor);
	cmBox7 = new MyComboBox(BackgroundColor);
	cmBox8 = new MyComboBox(BackgroundColor);
	cmBox9 = new MyComboBox(BackgroundColor);
	cmBox10 = new MyComboBox(BackgroundColor);

	this.setVisible(false);
	this.setBackground(BackgroundColor);
	this.setSize(800, 600);
	this.setLocation(50, 50);
	this.setLayout(null);
	this.add(bkg);

	// BACKGROUND
	bkg.setLayout(null);
	bkg.setSize(this.getSize());
	bkg.setLocation(0, 0);
	bkg.setBackground(BackgroundColor);
	bkg.add(frg);

	frg.setLayout(null);
	frg.setSize(this.getSize());
	frg.setLocation(0, 0);
	frg.setOpaque(false);
	frg.add(txtInstruct);
	for (int id = 1; id <= 10; id++) {
	    frg.add(this.getBox(id));
	}
	frg.setBackground(BackgroundColor);

	txtInstruct.setSize(new Dimension(600, 60));
	txtInstruct.setBackground(BackgroundColor);
	txtInstruct.setLineWrap(true);
	txtInstruct.setEditable(false);
	txtInstruct.setWrapStyleWord(true);
	txtInstruct.setLocation(100, 1);
	txtInstruct.setFont(new Font("Arial", Font.BOLD, 16));

	okButton.setText(btnText);
	okButton.setSize(new Dimension(100, 30));
	okButton.setLocation(Math.round((bkg.getWidth() - 100) / 2), 569);
	frg.add(okButton);

	loadImage(pictURL);
    }

    private void loadImage(URL imageURL) {
	Point posHandPict = new Point();
	ImageIcon img = new ImageIcon(imageURL);
	// create a label to paint the background image
	JLabel backgroundImage = new JLabel(img);
	backgroundImage.setSize(img.getIconWidth(), img.getIconHeight());
	posHandPict.x = Math.round((bkg.getWidth() - img.getIconWidth()) / 2);
	posHandPict.y = 85;
	backgroundImage.setLocation(posHandPict.x, posHandPict.y);
	// align the image as specified.
	backgroundImage.setVerticalAlignment(SwingConstants.TOP);
	backgroundImage.setHorizontalAlignment(SwingConstants.LEADING);
	// add the background label
	bkg.add(backgroundImage);

	initComboBox(1, posHandPict.x + 1, posHandPict.y + 160);
	initComboBox(2, posHandPict.x + 90, posHandPict.y + 32);
	initComboBox(3, posHandPict.x + 165, posHandPict.y);
	initComboBox(4, posHandPict.x + 225, posHandPict.y + 47);
	initComboBox(5, posHandPict.x + 260, posHandPict.y + 105);
	initComboBox(6, posHandPict.x + 345, posHandPict.y + 105);
	initComboBox(7, posHandPict.x + 380, posHandPict.y + 47);
	initComboBox(8, posHandPict.x + 440, posHandPict.y);
	initComboBox(9, posHandPict.x + 515, posHandPict.y + 32);
	initComboBox(10, posHandPict.x + 580, posHandPict.y + 160);

    }

    public void setInstruction(String str) {
	txtInstruct.setText(str);
    }

    public void setVisible(boolean show) {
	if (!show)
	    presentTime = 0;
	else if (presentTime == 0) {
	    Date now = new Date();
	    presentTime = now.getTime();
	}
	super.setVisible(show);
    }

    private long currMilliSec() {
	if (presentTime == 0)
	    return 0;
	else {
	    Date now = new Date();
	    return (now.getTime() - presentTime);
	}
    }

    private void initComboBox(int boxID, int xPos, int yPos) {
	// box.setBackground(Color.white);
	MyComboBox box = getBox(boxID);
	box.addActionListener(new Event(this, boxID));
	box.setLocation(xPos, yPos);
	box.removeAllItems();
	box.insertItemAt(" ", 0);
	int cnt = 1;
	for (int i = 1; i <= 10; i++) {
	    box.insertItemAt(Integer.toString(i), i);
	    cnt = cnt + 1;
	}
	box.setMaximumRowCount(cnt);
	box.setSelectedIndex(0);
    }

    private void updateBox(MyComboBox box) {
	if (box.getSelectedIndex() > 0) {
	    if (isDouble(box.getSelectedIndex()))
		box.setBackground(Color.red);
	    else
		box.setBackground(Color.white);
	} else
	    box.setBackground(MyComboBox.PULLDOWNCOLOR);
    }

    private boolean isDouble(int val) {
	int cnt = 0;
	for (int id = 1; id <= 10; id++) {
	    if (val == getBox(id).getSelectedIndex())
		cnt = cnt + 1;
	}
	return (cnt > 1);
    }

    public String getFingerSequenceString() {
	String rtn = "";
	for (int id = 1; id <= 10; id++) {
	    rtn = rtn + getBox(id).getSelectedIndex() + dataDelim;
	}
	return rtn;
    }

    public String getTimesString() {
	String rtn = "";
	for (int id = 1; id <= 10; id++) {
	    rtn = rtn + getBox(id).time + dataDelim;
	}
	return rtn;
    }

    private MyComboBox getBox(int id) {
	if (id == 1)
	    return cmBox1;
	else if (id == 2)
	    return cmBox2;
	else if (id == 3)
	    return cmBox3;
	else if (id == 4)
	    return cmBox4;
	else if (id == 5)
	    return cmBox5;
	else if (id == 6)
	    return cmBox6;
	else if (id == 7)
	    return cmBox7;
	else if (id == 8)
	    return cmBox8;
	else if (id == 9)
	    return cmBox9;
	else if (id == 10)
	    return cmBox10;
	else
	    return null;

    }

    class Event implements ActionListener {
	private FingerSequencePanel hdl;
	private int id;

	public Event(FingerSequencePanel theHdl, int theID) {
	    hdl = theHdl;
	    id = theID;
	}

	public void actionPerformed(ActionEvent e) {
	    MyComboBox pB = getBox(id);
	    pB.time = hdl.currMilliSec();
	    for (int id = 1; id <= 10; id++) {
		updateBox(hdl.getBox(id));
	    }
	} // actionPerformed
    }// event class
    // -------------------------------------------------------------------------------

}// main class

class MyComboBox extends JComboBox {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    public final static Color PULLDOWNCOLOR = Color.YELLOW;
    protected long time = 0;

    public MyComboBox(Color BackgroundColor) {
	super();
	setUI(new MyUI(BackgroundColor, PULLDOWNCOLOR));
	setSize(new Dimension(55, 30));
	setFont(new Font("Arial", Font.BOLD, 18));
	setFocusable(false);
    }

    class MyUI extends BasicComboBoxUI {

	private Color BGColor;

	public MyUI(Color BackgroundColor, Color PullDownCl) {
	    super();
	    BGColor = BackgroundColor;
	    setBackground(PullDownCl);
	    setForeground(Color.BLACK);
	}

	@Override
	protected ComboBoxEditor createEditor() {
	    ComboBoxEditor cbe = super.createEditor();
	    // cbe.getEditorComponent().setBackground(Color.BLACK);
	    // cbe.getEditorComponent().setForeground(Color.GREEN);
	    return cbe;
	}

	@Override
	protected JButton createArrowButton() {
	    return new MyBasicArrowButton(SwingConstants.BOTTOM, BGColor,
		    UIManager.getColor("ComboBox.buttonShadow"), UIManager
			    .getColor("ComboBox.buttonDarkShadow"), UIManager
			    .getColor("ComboBox.buttonHighlight"));
	}
    }

    class MyBasicArrowButton extends BasicArrowButton {
	/**
		 * 
		 */
	private static final long serialVersionUID = 1L;

	public MyBasicArrowButton(int direction) {
	    super(direction);
	}

	public MyBasicArrowButton(int direction, Color background,
		Color shadow, Color darkShadow, Color highlight) {
	    // super(direction,background,shadow,darkShadow,highlight);
	    super(direction, background, background, background, background);
	    // setBackground(Color.BLACK);
	}

	@Override
	public void paintTriangle(Graphics g, int x, int y, int size,
		int direction, boolean isEnabled) {
	    Color oldColor = g.getColor();
	    int mid, i, j;
	    j = 0;
	    size = Math.max(size, 2);
	    mid = (size / 2) - 1;
	    g.translate(x, y);
	    g.setColor(Color.gray);
	    j = 0;
	    for (i = size - 1; i >= 0; i--) {
		g.drawLine(mid - i, j, mid + i, j);
		j++;
	    }
	    g.translate(-x, -y);
	    g.setColor(oldColor);
	}
    }
} // main class

